package com.zhouyer.study.rpc.consumer.controller;

import com.zhouyer.study.rpc.api.IUserService;
import com.zhouyer.study.rpc.consumer.proxy.RpcClientProxy;
import com.zhouyer.study.rpc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * RPC控制器
 *
 * @author zhouyao
 * @version 1.0
 * @date 2021/11/10 1:40 下午
 */
@RestController
public class RpcController {
    @Autowired
    private RpcClientProxy rpcClientProxy;

    @GetMapping("/user/{id}")
    public User getUsersById(@PathVariable int id, HttpServletRequest request) {
        IUserService userService = (IUserService) rpcClientProxy.createProxy(IUserService.class);
        User user = userService.getById(id);
        user.setPort(request.getLocalPort());
        return user;
    }
}
